package GreedyAlgorithmPackage;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.OptionalInt;

/**
 * @author Lzm
 * @version 1.0
 */
public class wiggleMaxLength_ {

  @Test
  public void wiggleMaxLengthTest(){
    int[] nums = new int[]{0,0,0};
    //int[] nums = new int[]{1,17,5,10,13,15,10,5,16,8};
    //int[] nums = new int[]{1,1,1,2,3,4,4,-1,-1,2,3,3,3,3};
    int result = wiggleMaxLength(nums);
    System.out.println(result);
  }

  public int wiggleMaxLength(int[] nums) {
    int[] newArray = turnArray(nums);
    List<Integer> list = new ArrayList<>();
    int result = 1;
    if (newArray.length == 1){
      return result;
    }
    if (newArray.length == 2 && newArray[0] != newArray[1]){
      return result + 1;
    }
    else if (newArray.length == 2 && newArray[0] == newArray[1]){
      return result;
    }

    for (int i = 1 ; i < newArray.length - 1 ; i ++){
      if (newArray[i] > newArray[i - 1] && newArray[i] > newArray[i + 1]){
        result++;
      }
      else if (newArray[i] < newArray[i - 1] && newArray[i] < newArray[i + 1]){
        result++;
      }
      else{
        continue;
      }
    }
    return result + 1;
  }
  public int[] turnArray(int[] nums){
    List<Integer> list = new ArrayList<>();
    int pointer = 1;
    for (int i = 0 ; i < nums.length - 1;){
      if (nums[i] != nums[pointer]){
        list.add(nums[i]);
        i++;
        pointer++;
        continue;
      }
      while(pointer < nums.length && nums[i] == nums[pointer]){
        i++;
        pointer++;
      }
    }
    list.add(nums[nums.length - 1]);
    return list.stream().mapToInt(Integer::intValue).toArray();
  }
}
